package me.demo;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;


public class App {
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();
        loop:
        while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择操作 1登录 2注册 3忘记密码 4退出");
            Scanner sc = new Scanner(System.in);
            int choice = sc.nextInt();
            switch (choice) {
                case 1 -> login(list);
                case 2 -> register(list);
                case 3 -> forgetPassword(list);
                case 4 -> {
                    System.out.println("退出");
                    break loop;
                }
                default -> System.out.println("您的输入有误");
            }
        }
    }

    private static void login(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入用户名");
            String username = sc.next();
            boolean falg = contains(list, username);
            if (!falg) {
                System.out.println("用户未注册,请先注册");
                return;
            }
            System.out.println("请输入密码");
            String password = sc.next();
            while (true) {
                String rightCode = getCode();
                System.out.println("正确的验证码为" + rightCode);
                System.out.println("请输入验证码");
                String code = sc.next();
                if (rightCode.equals(code)) {
                    System.out.println("验证码正确");
                    break;
                } else {
                    System.out.println("验证码错误");
                    continue;
                }
            }
            boolean result = checkUser(list, username, password);
            if (result) {
                System.out.println("登录成功");

            } else {
                System.out.println("登录失败");
            }
            if(i==2){
                System.out.println("3次登录失败,请稍后再试");
                return;
            }
            else{
                System.out.println("还剩下"+(2-i)+"次机会");
            }
        }

    }

    private static boolean checkUser(ArrayList<User> list, String username, String password) {
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            if (u.getUsername().equals(username) && u.getPassword().equals(password)) {
                return true;
            }
        }
        return false;
    }

    private static String getCode() {
        ArrayList<Character> list = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            list.add((char) ('a' + i));
            list.add((char) ('A' + i));
        }
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int index = random.nextInt(list.size());
            sb.append(list.get(index));
        }
        int number = random.nextInt(10);
        sb.append(number);
        char[] arr = sb.toString().toCharArray();
        int index1 = random.nextInt(arr.length);
        int index2 = random.nextInt(arr.length);
        char temp = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = temp;
        return new String(arr);
    }

    private static void register(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        String username;
        String password;
        String ID;
        String phone;
        while (true) {
            System.out.println("请输入用户名");
            username = sc.next();
            boolean flag1 = checkUsername(username);
            if (!flag1) {
                System.out.println("用户名格式错误");
                continue;
            }
            boolean flag2 = contains(list, username);
            if (flag2) {
                System.out.println("用户名已存在,请重新输入");
            } else {
                System.out.println("用户名格式正确");
                break;
            }
        }
        while (true) {
            System.out.println("请输入密码");
            password = sc.next();
            System.out.println("请再次输入密码");
            String again = sc.next();
            if (!password.equals(again)) {
                System.out.println("两次密码不一致");
                continue;
            } else {
                System.out.println("密码可用");
                break;
            }
        }
        while (true) {
            System.out.println("请输入身份证号");
            ID = sc.next();
            boolean flag3 = checkID(ID);
            if (flag3) {
                System.out.println("身份证号可用");
                break;
            } else {
                System.out.println("身份证号格式错误");
            }
        }
        while (true) {
            System.out.println("请输入手机号");
            phone = sc.next();
            boolean flag4 = checkPhone(phone);
            if (flag4) {
                System.out.println("手机号可用");
                break;
            } else {
                System.out.println("手机号格式错误");
            }
        }
        User u = new User(username, password, ID, phone);
        list.add(u);
        System.out.println("注册成功");
        printList(list);
    }

    private static void printList(ArrayList<User> list) {
        System.out.println("用户名\t密码\t身份证号\t手机号");
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            System.out.println(u.getUsername() + "\t" + u.getPassword() + "\t" + u.getID() + "\t" + u.getPhone());
        }
    }

    private static void forgetPassword(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.next();
        boolean falg = contains(list, username);
        if (!falg) {
            System.out.println("用户未注册,请先注册");
            return;
        }
        System.out.println("请输入身份证号");
        String ID=sc.next();
        System.out.println("请输入手机号");
        String phone=sc.next();
        int index=getIndex(list,username);
        User us=list.get(index);
        if(!(us.getID().equals(ID)&&us.getPhone().equals(phone))){
            System.out.println("信息错误");
            return;
        }
        String password;
        while (true) {
            System.out.println("请输入新的密码");
            password=sc.next();
            System.out.println("请再次输入新的密码");
            String againpassword=sc.next();
            if(password.equals(againpassword)){
                System.out.println("两次密码一致");
                break;
            }
            else{
                System.out.println("重新输入");
                continue;
            }
        }
        us.setPassword(password);
        System.out.println("密码修改成功");
    }

    private static boolean checkUsername(String username) {
        if (username.length() < 3 || username.length() > 10) {
            System.out.println("用户名长度必须在3-10位之间");
            return false;
        }
        int count = 0;
        for (int i = 0; i < username.length(); i++) {
            char c = username.charAt(i);
            if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
                count++;
                break;
            }
        }
        return count > 0;
    }

    private static boolean contains(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            if (u.getUsername().equals(username)) {
                return true;
            }
        }
        return false;
    }

    private static boolean checkID(String ID) {
        if (ID.length() != 18) {
            System.out.println("身份证长度错误");
            return false;
        }
        if (ID.startsWith("0")) {
            System.out.println("身份证不能以0开头");
            return false;
        }
        for (int i = 0; i < ID.length() - 1; i++) {
            char c = ID.charAt(i);
            if (!(c >= '0' && c <= '9')) {
                System.out.println("身份证必须是数字");
                return false;
            }
        }
        char c = ID.charAt(17);
        if (!(c == 'X' || c == 'x' || (c >= '0' && c <= '9'))) {
            System.out.println("身份证最后一位必须是X,x或数字");
            return false;
        }
        return true;
    }

    private static boolean checkPhone(String phone) {
        if (phone.length() != 11) {
            System.out.println("手机号长度错误");
            return false;
        }
        if (phone.startsWith("0")) {
            System.out.println("手机号不能以0开头");
            return false;
        }
        for (int i = 0; i < phone.length(); i++) {
            char c = phone.charAt(i);
            if (!(c >= '0' && c <= '9')) {
                System.out.println("手机号必须是数字");
                return false;
            }
        }
        return true;
    }

    private static int getIndex(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            if (u.getUsername().equals(username)) {
                return i;
            }
        }
        return -1;
    }
}
